package com.qianchang.microfinance.view.mine.bill;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.MyBillBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.base.CustomRecyclerViewActivity;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.dm.library.widgets.statusbar.StatusBarUtil;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的账单
 */
public class MyBillActivity extends CustomRecyclerViewActivity<MyBillBean, MyBillBean.Bean> {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_total)
    DTextView tvTotal;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bill);
        StatusBarUtil.setTranslucentForImageViewInFragment(this, null);
        ButterKnife.bind(this);
        setTitleName(getString(R.string.title_my_bill));
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void handleResult(JsonResult<MyBillBean> response) {
        if (response.isReqDataSuccess()) {
            MyBillBean bean = response.getObject();
            if (bean != null) {
                refreshUi(bean);
                if (bean.getPageResult() != null && bean.getPageResult().getList() != null) {
                    setListData(bean);
                    mAdapter.notifyDataSetChanged();
                }
            } else {
                mAdapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
            }
        } else {
            ToastUtil.getInstant().show(this, response.getDescription());
        }
    }

    private void refreshUi(MyBillBean bean) {
        if (!TextUtils.isEmpty(bean.getPayTotalAmount())) {
            tvTotal.setText(FormatUtil.numKbPointFormat(bean.getPayTotalAmount()));
        }
    }

    @Override
    protected void requestData(HttpParams params) {
        params.put("opSource", 1);   //1.pc;4.android;3.苹果
        params.put("maxResults", 10); //分页参数
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.GET_BILL_LIST, params, mCallBack);
    }

    @Override
    protected int getPageIndex() {
        return currentPageIndex;
    }

    @Override
    protected void setListData(MyBillBean myBillBean) {
        if (myBillBean == null) return;
        MyBillBean.PageResult pageResult = myBillBean.getPageResult();
        List<MyBillBean.Bean> mList = pageResult.getList();
//        mAdapter.addAll(mList);
        if (isRefreshing) {//刷新
            mAdapter.clear();
            mAdapter.addAll(mList);
            isRefreshing = false;
        } else {//加载更多
            mAdapter.addAll(mList);
        }
        currentPageIndex = pageResult.getPageIndex() + 1;
        boolean hasNextPage = pageResult.getHasNextPage();
        if (hasNextPage) {
            mAdapter.setState(BaseRecyclerAdapter.STATE_LOADING, true);
        } else {
            mAdapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
        }
        mRefreshLayout.setCanLoadMore(hasNextPage);
    }

    @Override
    protected BaseRecyclerAdapter<MyBillBean.Bean> getRecyclerAdapter() {
        return new MyBillAdapter(this);
    }

    @Override
    protected Type getType() {
        return new TypeToken<MyBillBean>() {
        }.getType();
    }
}
